<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21
 * Time: 14:42
 */

namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Member extends Adminbase
{

    /**
     * 会员管理页面
     * */
    public function index()
    {
        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);
            $where = [];
            $order = "id DESC";

            $mobile = input('mobile');
            if($mobile){
                $where[] = ['mobile','=',$mobile];
            }
            $time = input('time');
            if($time){
                $start_time = substr($time, 0, 20);//订单那创建开始时间
                $end_time = substr($time, 21, 20);//订单那创建开始时间
                $time = strtotime($start_time);
                $times = strtotime($end_time);
                $where[] = ['create_time','between',$time.','.$times];
            }
            $role_id = input('role_id');
            if($role_id){
                $where[] = ['role_id','=',$role_id];
            }

            $memberList = Db::name('member')
                ->where($where)
                ->page($page, $limit)
                ->order($order)
                ->withAttr('create_time', function ($value, $data) {
                    if($value){
                        return date('Y-m-d H:i:s', $value);
                    }else{
                        return "--";
                    }
                })
                ->withAttr('sex', function ($value, $data) {
                    if($value == 1){
                        $sexName = "男";
                    }else{
                        $sexName = "女";
                    }
                    return $sexName;
                })
                ->withAttr('role_id', function ($value, $data) {
                    if($value == 1){
                        $role = "普通用户";
                    }else if($value == 2){
                        $role = "一级管理";
                    }else if($value == 3){
                        $role = "志愿者";
                    }else{
                        $role = "二级管理";
                    }
                    return $role;
                })
                ->select();

            $total = Db::name('member')
                ->where($where)
                ->order($order)
                ->count();
            $result = array("code" => 0, "count" => $total, "data" => $memberList);
            return json($result);

        }
        return $this->fetch();

    }

    /**
     * 会员冻结
     * */
    public function del()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据失败');
        }

        $status = Db::name('member')->where(['id' => $id])->value('status');
        if($status == -1){
            $this->error('该账号已被冻结');
        }

        $data = array(
            'status' => -1
        );
        $res = Db::name('member')->where(['id' => $id])->update($data);
        if($res){
            $this->error('冻结成功');
        }else{
            $this->error('冻结失败');
        }

    }

    /**
     * 会员恢复
     * */
    public function reUser()
    {

        $id = input('id');
        if(!$id){
            $this->error('获取数据失败');
        }

        $status = Db::name('member')->where(['id' => $id])->value('status');
        if($status == 1){
            $this->error('该账号正常');
        }

        $data = array(
            'status' => 1
        );
        $res = Db::name('member')->where(['id' => $id])->update($data);
        if($res){
            $this->error('恢复成功');
        }else{
            $this->error('恢复失败');
        }

    }



    /**
     * 会员编辑
     */
    public function edit()
    {
        $member = new \app\admin\model\Member();
        if ($this->request->isPost()) {
            $data = $this->request->post('');
//            $result = $this->validate($data, 'AdminUser.update');
//            if (true !== $result) {
//                return $this->error($result);
//            }
            if ($member->editMember($data)) {
                $this->success("修改成功！");
            } else {
                $this->error($this->User->getError() ?: '修改失败！');
            }
        } else {
            $id = $this->request->param('id/d');
            $list = Db::name('zhibu')->field('value')->select();
            $data = $member->where(array("id" => $id))->find();
            if (empty($data)) {
                $this->error('该信息不存在！');
            }
            $arr = [];
            foreach ($list as $k => $v){
                array_push($arr,$v['value']);
            }
            $zhiId = Db::name('zhibu')->where(['value' => $data['danwei']])->value('id');
            $zhi = Db::name('zhibu_list')->where(['pid'=>$zhiId])->field('value')->select();

            $this->assign("zhi",$zhi);
            $this->assign("data", $data);
            $this->assign("fieldList", $arr);
            return $this->fetch();
        }
    }


    /**
     * 会员添加
     */
    public function add()
    {
        $member = new \app\admin\model\Member();
        if ($this->request->isPost()) {
            $data = $this->request->post('');
//            $result = $this->validate($data, 'Member.insert');
//            if (true !== $result) {
//                return $this->error($result);
//            }
            if(empty($data['zhibu'])){
                $this->error('请选择用户支部');
            }
            if ($member->createMember($data)) {
                $this->success("添加管理员成功！", url('admin/member/index'));
            } else {
                $error =$member->getError();
                $this->error($error ? $error : '添加失败！');
            }

        } else {
            $list = Db::name('zhibu')->field('value')->select();
            $arr = [];
            foreach ($list as $k => $v){
                array_push($arr,$v['value']);
            }
            $this->assign("fieldList", $arr);
            return $this->fetch();
        }
    }


    /**
     * 获取支部信息
     * */
    public function getZhi()
    {

        $value = input('value');

        $where[] = ['value','=',$value];
        $id = Db::name('zhibu')->where($where)->value('id');

        $list = Db::name('zhibu_list')->where(['pid' => $id])->field('value')->select();
        $data = [];
        foreach ($list as $k => $v){
            array_push($data,$v['value']);
        }
        $this->result($data,'1','获取数据成功','json');

    }


}